﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class 到货数据校验 : System.Web.UI.Page
    {
        protected string msg = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
            if (this.LaiYuan.Text == "全部")
            {
                this.Button_Query.Text = "校验";
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        private string GetState()
        {
            DateTime d1, d2;
            if (!DateTime.TryParse(this.Date1.Text, out d1))
            {
                return null;
            }

            if (!DateTime.TryParse(this.Date2.Text, out d2))
            {
                return null;
            }
            string state = " where 0=0";
            state = " and t1.供货ID=" + this.GongHuo.Text
                + " and t1.原料ID=" + this.YuanLiao.Text                
                + " and t1.到货日期 between '" + d1.ToShortDateString() + "' and '" + d2.ToShortDateString() + "'";
            if (this.weight.Text != "")
            {
                state += " and t1.总重量=" + this.weight.Text;
            }
            return state;
        }
        private string GetState2()
        {
            DateTime d1, d2;
            DateTime.TryParse(this.Date1.Text, out d1);
            DateTime.TryParse(this.Date2.Text, out d2);
            string state = " where 0=0";
            state = " and t1.供货ID=" + this.GongHuo.Text
                + " and t1.原料ID=" + this.YuanLiao.Text          
                + " and t1.到货日期 between '" + d1.ToShortDateString() + "' and '" + d2.ToShortDateString() + "'";
            if (this.weight.Text != "")
            {
                state += " and t1.总重量=" + this.weight.Text;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            if (state == null)
            {
                this.msg = "请输入日期范围";
                return;
            }
            this.SqlText.Text =
                    @"select t3.到货日期,t3.数据,t3.原料,t3.供货单位,t3.总重量,t3.磅重,t3.磅差,t3.总支数
                     from 
                    (select t1.到货日期 ,'供应处' as 数据, case 
                    when t1.原料ID=1 then '油管光管(J55)' 
                    when t1.原料ID=2 then '套管光管(J55)' 
                    when t1.原料ID=3 then '油管接箍光管(J55)' 
                    when t1.原料ID=4 then '套管接箍光管(J55)' 
                    when t1.原料ID=5 then '成品油管接箍(J55)' 
                    when t1.原料ID=6 then '成品套管接箍(J55)' 
                    when t1.原料ID=101 then '油管光管(N80)' 
                    when t1.原料ID=102 then '套管光管(N80)' 
                    when t1.原料ID=103 then '油管接箍光管(N80)' 
                    when t1.原料ID=104 then '套管接箍光管(N80)' 
                    when t1.原料ID=105 then '成品油管接箍(N80)' 
                    when t1.原料ID=106 then '成品套管接箍(N80)' 
                    else '其他原料' end as 原料 ,t2.名称 as 供货单位 
                    ,t1.总重量
                    ,t1.磅重 ,t1.磅重-sum(采购原料到货明细.重量) as 磅差 ,
                    t1.总支数 
                    from 采购原料到货记录 
                    t1 left outer join 原料供货单位 t2 on t2.ID=t1.供货ID inner join 
                    采购原料到货明细 on 采购原料到货明细.记录ID=t1.ID"
                    + GetState() +
                    @"group by t1.ID,t1.原料ID,t1.供货ID,t1.分厂,t1.库区,t1.到货日期,t1.总重量,t1.磅重,t1.总支数,t2.名称
                    union all
                    select t1.到货日期,'物流中心' as 数据, case 
                    when t1.原料ID=1 then '油管光管(J55)' 
                    when t1.原料ID=2 then '套管光管(J55)' 
                    when t1.原料ID=3 then '油管接箍光管(J55)' 
                    when t1.原料ID=4 then '套管接箍光管(J55)' 
                    when t1.原料ID=5 then '成品油管接箍(J55)' 
                    when t1.原料ID=6 then '成品套管接箍(J55)' 
                    when t1.原料ID=101 then '油管光管(N80)' 
                    when t1.原料ID=102 then '套管光管(N80)' 
                    when t1.原料ID=103 then '油管接箍光管(N80)' 
                    when t1.原料ID=104 then '套管接箍光管(N80)' 
                    when t1.原料ID=105 then '成品油管接箍(N80)' 
                    when t1.原料ID=106 then '成品套管接箍(N80)'                    
                    else '其他原料' end as 原料 ,t2.名称 as 供货单位 
                    ,t1.总重量
                    ,t1.磅重 ,t1.磅重-sum(原料到发明细.重量) as 磅差 ,
                    t1.总支数 
                    from 原料到发记录 
                    t1 left outer join 原料供货单位 t2 on t2.ID=t1.供货ID inner join 
                    原料到发明细 on 原料到发明细.记录ID=t1.ID"
                    + GetState2() +
                    @" group by t1.ID,t1.原料ID,t1.供货ID,t1.分厂,t1.库区,t1.到货日期,t1.总重量,t1.磅重,t1.总支数,t2.名称) t3
                    group by t3.到货日期,t3.数据,t3.原料,t3.供货单位,t3.总重量,t3.磅重,t3.磅差,t3.总支数
                    @having
                    order by 到货日期 desc";
            this.SqlDataSource1.SelectCommand = "";
            if (this.LaiYuan.Text == "全部")
            {
                this.SqlText.Text=this.SqlText.Text.Replace("@having","");
            }
            if (this.LaiYuan.Text == "供应处")
            {
                this.SqlText.Text = this.SqlText.Text.Replace("@having", " having t3.数据='"+this.LaiYuan.Text+"'");
            }
            if (this.LaiYuan.Text == "物流中心")
            {
                this.SqlText.Text = this.SqlText.Text.Replace("@having", " having t3.数据='" + this.LaiYuan.Text + "'");
            }

            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void LaiYuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.LaiYuan.Text == "全部")
            {
                this.Button_Query.Text = "校 验";
            }
            else
            {
                this.Button_Query.Text = "查询";
            }
        }
    }
}
